//
//  AppDelegate.m
//  GoodsBTS
//
//  Created by onemade on 14-9-18.
//  Copyright (c) 2014年 CNPC. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()<UIActionSheetDelegate,CLLocationManagerDelegate>

@end

@implementation AppDelegate

- (void)applicationWillResignActive:(UIApplication *)application {
    
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    
}

- (void)applicationWillTerminate:(UIApplication *)application {
    
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [BeaconAdvertisingService sharedInstance];
    
    [[BeaconMonitoringService sharedInstance] startMonitoringBeaconWithUUID:[[NSUUID alloc] initWithUUIDString:iBeacon_UUID] major:1 minor:10000 identifier:iBeacon_Identifier onEntry:YES onExit:YES];
    
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    
//    self.tabBarController = (UITabBarController *)self.window.rootViewController;
//    BOOL isFirstLaunch = [[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstLaunch"];
//    if (!isFirstLaunch) {
//        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"请选择设备类型" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"产品端" otherButtonTitles:@"客户端", nil];
//        [actionSheet showInView:self.window];
//    }
//    else{
//        NSInteger selectIndex  = [[NSUserDefaults standardUserDefaults] integerForKey:@"launchIndex"];
//        [self.tabBarController setSelectedIndex:selectIndex];
//    }
    return YES;
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex != actionSheet.cancelButtonIndex) {
        [self.tabBarController setSelectedIndex:buttonIndex];
        [[NSUserDefaults standardUserDefaults] setInteger:buttonIndex forKey:@"launchIndex"];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isFirstLaunch"];
    }
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    UITabBarController * tabBarController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"GoodsMainTabBar"];
    tabBarController.selectedViewController = tabBarController.viewControllers[1];
}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    self.tabBarController = (UITabBarController *)self.window.rootViewController;
    [self.tabBarController setSelectedIndex:0];
}

#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = [NSString stringWithFormat:@"亲，您正在靠近 —— %@!", region.identifier];
    notification.soundName = @"Default";
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = [NSString stringWithFormat:@"欢迎浏览——%@", region.identifier];
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
    for (CLBeacon *beacon in beacons) {
        if (beacon.minor == region.minor) {
            if (beacon.proximity == CLProximityImmediate) {
                
            }
        }
    }
}

-(NSMutableArray *)GoodsList
{
    if (_GoodsList) {
        return _GoodsList;
    }
    _GoodsList = [[NSMutableArray alloc] initWithCapacity:20];
    NSData *goods = [NSData dataWithContentsOfURL:[NSURL URLWithString:GoodsListURL]];
    [self fillArrayWithData:goods];
    return _GoodsList;
}


-(void)fillArrayWithData:(NSData *)data
{
    if (data != nil) {
        id jsonData = nil;
        NSError * error= nil;
        jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        for (NSDictionary *dict in jsonData) {
            NSString *goodid = [dict objectForKey:@"goodid"];
            NSString *title = [dict objectForKey:@"title"];
            NSString *price = [dict objectForKey:@"price"];
            NSString *remark = [dict objectForKey:@"mark"];
            
            _goodDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                               goodid,@"goodid",
                              title,@"title",
                              price,@"price",
                              remark,@"remark",
                              nil];
            [_GoodsList addObject:_goodDictionary];
        }
    }
}
@end
